package com.han.spider.cloudmeeting.websocket;

import com.han.spider.cloudmeeting.MeetingManager;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author H
 */
@Slf4j
public class WebsocketClient extends WebSocketClient {

    private String userId;

    public WebsocketClient(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders, int connectTimeout) {
        super(serverUri, protocolDraft, httpHeaders, connectTimeout);
    }

    public WebsocketClient(URI serverUri) {
        super(serverUri);
    }

    public WebsocketClient(URI serverUri, Draft protocolDraft, String userId) {
        super(serverUri, protocolDraft);
        this.userId = userId;
        setConnectionLostTimeout(Integer.MAX_VALUE);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info(userId + ":" + "打开链接");
    }

    @Override
    public void onMessage(String message) {
        log.info(userId + "-接受数据:" + message);
        try {
            JSONObject jsonObject = new JSONObject(message);
            int id = jsonObject.getInt("id");
            if (id == 0) {
                JSONObject resultObject = jsonObject.getJSONObject("result");
                MeetingManager.gid = resultObject.getString("gid");
            } else if (id == 1) {
                JSONObject resultObject = jsonObject.getJSONObject("result");
                JSONArray userArray = resultObject.getJSONArray("userlist");
                List<String> uIdList = new ArrayList<>();
                for (int i = 0; i < userArray.length(); i++) {
                    JSONObject userObj = userArray.getJSONObject(i);
                    String uid = userObj.getString("nid");
                    uIdList.add(uid);
                }
                MeetingManager.uIdList = uIdList;
                log.info(userId + ":解析人员列表成功,size:" + MeetingManager.uIdList);
            } else if (id == 2) {
                JSONObject resultObject = jsonObject.getJSONObject("result");
                String nid = resultObject.getString("nid");
                MeetingManager.nid = nid;
                if (!nid.isBlank()) {
                    log.info(userId + ":" + "创建会议成功");
                }
            } else if (id == 3) {
                String result = jsonObject.getString("result");
                if ("success".equals(result)) {
                    log.info(userId + ":" + "setAdviseMedia成功");
                }
            } else if (id == 4) {
                String result = jsonObject.getString("result");
                if ("success".equals(result)) {
                    log.info(userId + ":" + "setMember成功");
                }
            } else if (id == 5) {
                JSONObject resultJson = jsonObject.getJSONObject("result");
                JSONArray callArray = resultJson.getJSONArray("calls");
                List<String> list = new ArrayList<>();
                for (int i = 0; i < callArray.length(); i++) {
                    JSONObject tempJson = callArray.getJSONObject(i);
                    String meetingId = tempJson.getString("nid");
                    list.add(meetingId);
                }
                MeetingManager.meetingIdList = list;
                log.info(userId + ":解析会议列表成功,size:" + MeetingManager.meetingIdList);
            }
        } catch (JSONException e) {
            log.error(userId + ":" + message, e);
        }
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        log.info(userId + ":" + "链接已关闭:" + i + "," + s + "," + b);
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
        log.info(userId + ":" + "发生错误已关闭");
    }

}